/*
 * Miccsnookersim, a snooker simulation with AI
 * Copyright (C) 2009 Enno Ruijters, Frans van den Heuvel, Yannick Thimister,
 * Ali B. Ozmen, Esther Verhoef, Achim Leydecker
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Settings extends JFrame
{
	private JSlider forceNoiseSlider, PoCNoiseSlider, angleNoiseSlider,
	                frictionNoiseSlider;
	private JButton OKButton, cancelButton;
	private double forceNoise, PoCNoise, angleNoise, frictionNoise;

	public synchronized double getForceNoise()
	{
		return forceNoise;
	}

	public synchronized double getPoCNoise()
	{
		return PoCNoise;
	}

	public synchronized double getAngleNoise()
	{
		return angleNoise;
	}

	public synchronized double getFrictionNoise()
	{
		return frictionNoise;
	}

	class OKButtonListener implements ActionListener
	{
		private JFrame parent;

		public OKButtonListener(JFrame parent)
		{
			this.parent = parent;
		}

		public void actionPerformed(ActionEvent e)
		{
			synchronized(parent) {
				parent.setVisible(false);
				forceNoise = forceNoiseSlider.getValue() / 5;
				PoCNoise = PoCNoiseSlider.getValue() / 10000.0;
				angleNoise = angleNoiseSlider.getValue() / 10000.0;
				frictionNoise = frictionNoiseSlider.getValue() / 10.0;
			}
		}
	}

	class CancelButtonListener implements ActionListener
	{
		private JFrame parent;

		public CancelButtonListener(JFrame parent)
		{
			this.parent = parent;
		}

		public void actionPerformed(ActionEvent e)
		{
			synchronized(parent) {
				parent.setVisible(false);
				forceNoiseSlider.setValue((int) (forceNoise / 5));
				PoCNoiseSlider.setValue((int) (PoCNoise * 10000));
				angleNoiseSlider.setValue((int)(angleNoise * 10000));
				frictionNoiseSlider.setValue((int) frictionNoise);
			}
		}
	}
			

	public Settings()
	{
		forceNoise = 1;
		PoCNoise = Math.PI / 1440;
		angleNoise = Math.PI / 1440;
		frictionNoise = 3;
		getContentPane().setLayout(new GridLayout(5,2));
		forceNoiseSlider = new JSlider(0, 100, (int) (forceNoise * 5));
		PoCNoiseSlider = new JSlider(0, 100, (int) (PoCNoise * 10000));
		angleNoiseSlider = new JSlider(0, 100, (int)(angleNoise * 10000));
		frictionNoiseSlider = new JSlider(0, 100, (int) frictionNoise * 10);
		add(new JLabel("Cue force noise:"));
		add(forceNoiseSlider);
		add(new JLabel("Point of contact noise:"));
		add(PoCNoiseSlider);
		add(new JLabel("Cue angle noise:"));
		add(angleNoiseSlider);
		add(new JLabel("Friction noise:"));
		add(frictionNoiseSlider);
		OKButton = new JButton("OK");
		OKButton.addActionListener(new OKButtonListener(this));
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new CancelButtonListener(this));
		add(OKButton);
		add(cancelButton);
	}
}
